package com.compass.doctor.ui.prescription.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.WebDetailActivity
import com.compass.doctor.ui.patient.WebRXExamineActivity
import com.compass.doctor.ui.prescription.adapter.RxExamineAdapter
import com.compass.doctor.ui.prescription.viewmodel.PrescribeViewModel
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.REFRESH_RX
import com.compass.framework.constant.REFRESH_RX_EXAMINE
import com.compass.framework.constant.RX_DETAILS
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 历史处方--药师
 * Date:2024/2/21 14:51
 */
class HistoryRxFragment : BaseVBFragment<FragmentBaseListBinding>(), OnLoadMoreListener,
    OnRefreshListener {

    private val prescribeViewModel: PrescribeViewModel by viewModels()
    private lateinit var mAdapter: RxExamineAdapter
    private var mPage = 1
    private var orderType: Int = 1
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            orderType = it.getInt(KEY_ORDER_TYPE)
        }
        mBinding?.llBottomBtn?.gone()

        initRecyclerView()
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@HistoryRxFragment)
            setOnLoadMoreListener(this@HistoryRxFragment)
        }
        mAdapter = RxExamineAdapter()
        val dp10 = dpToPx(10)
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(left = dp10, top = dp10, right = dp10, bottom = dp10)
                setLastBottom(true)
            })
            adapter = mAdapter
        }

        mAdapter.setBottomCallBack(object : OnCustomButtonClick {
            override fun onButtonCallBack(position: Int, msgType: String?) {
                val item=mAdapter.getItem(position)
                when(msgType){
                    "details"->{
                        item?.let {details->
                            WebDetailActivity.start(
                                context = requireActivity(),
                                url = RX_DETAILS +details.id,
                                title ="处方详情"
                            )
                        }


                    }
                }
            }
        })


    }


    override fun initData() {
        showLoading()
        getHisRx()
        LiveDataBus.get().with(REFRESH_RX_EXAMINE, String::class.java).observe(this) {
            getHisRx()
        }
        prescribeViewModel.prescriptionRecordLiveData.observe(this) {
            dismissLoading()
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
    }


    private fun getHisRx() {
        prescribeViewModel.getRxExamine(mPage,"2")
    }


    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getHisRx()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getHisRx()
    }
}